<template>
    <el-form :model="mfl" :rules="rules" ref="mfl" label-width="100px">
        <p>手机登录</p>
        <el-form-item label="手机" prop="mobile">
            <el-input v-model="mfl.mobile"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="mfl.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('mfl')">登录</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    export default {
        head() {
            return {title: '手机登录'};
        },
        data() {
            return {
                mfl: {
                    mobile: '',
                    password: ''
                },
                rules: {
                    mobile: [
                        {required: true, message: '请填写手机', trigger: 'change'}
                    ],
                    password: [
                        {required: true, message: '请填写密码', trigger: 'change'},
                        {type: 'string', min: 1, message: '密码不能为空', trigger: 'change'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$store.dispatch('user/mobile', {
                            mobile: this[formName].mobile,
                            password: this[formName].password
                        }).then(() => {
                            this.$message({message: '登录成功', type: 'success'});
                            this.$router.push('/admin');
                        }).catch((e) => {
                            this.$message({message: '登录失败', type: 'error'});
                        });
                    } else {
                        return false;
                    }
                });
            }
        }
    };
</script>